package com.woniuxy.controller;

import com.woniuxy.entity.DTO.*;
import com.woniuxy.entity.GoodsCommit;
import com.woniuxy.service.impl.OrderServiceImpl;
import com.woniuxy.util.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/orderUser")
public class OrderUserController {
    @Resource
    private OrderServiceImpl orderService;
    @PostMapping("/queryOrder")
    public ResponseData queryOrder(@RequestBody(required = false) Integer index){
        try {
            if (index==null){
                index=0;
            }
            List<OrderUserDTO> orderUserDTOS = orderService.queryOrderByStatus(index);
            if (orderUserDTOS != null) {
                return ResponseData.success(orderUserDTOS);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return ResponseData.success();
    }
    @GetMapping("/deleteOrder/{orderId}")
    public ResponseData deleteOrder(@PathVariable("orderId") String orderId){
        try {
            orderService.deleteUserOrder(orderId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResponseData.success();
    }
    @GetMapping("/queryOrderItem/{orderId}")
    public ResponseData queryOrderItem(@PathVariable("orderId") String orderId){
        OrderItemAndAddressDTO orderItemAndAddressDTO = orderService.queryOrderItemAndAddressByOrderId(orderId);
        return ResponseData.success(orderItemAndAddressDTO);
    }
    @GetMapping("/queryOrderByOrderId/{orderId}")
    public ResponseData queryOrderByOrderId(@PathVariable("orderId") String orderId){
        List<GoodsImageSrcDTO> orders = orderService.queryImgSrc(orderId);



        return ResponseData.success(orders);
    }
    @PostMapping("/addComment")
    public ResponseData addComment(@RequestBody CommitdDTO commitdDTO){
        try {

            return  orderService.addComment(commitdDTO);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

}
